package com.apollographql.apollo.internal;

import com.apollographql.apollo.api.GraphqlFragment;
import com.apollographql.apollo.api.Operation;
import com.apollographql.apollo.api.Response;
import com.apollographql.apollo.api.ResponseField;
import com.apollographql.apollo.api.ScalarTypeAdapters;
import com.apollographql.apollo.api.internal.ApolloLogger;
import com.apollographql.apollo.api.internal.ResponseFieldMapper;
import com.apollographql.apollo.api.internal.Utils;
import com.apollographql.apollo.cache.CacheHeaders;
import com.apollographql.apollo.cache.normalized.ApolloStore;
import com.apollographql.apollo.cache.normalized.ApolloStoreOperation;
import com.apollographql.apollo.cache.normalized.CacheKey;
import com.apollographql.apollo.cache.normalized.CacheKeyResolver;
import com.apollographql.apollo.cache.normalized.NormalizedCache;
import com.apollographql.apollo.cache.normalized.OptimisticNormalizedCache;
import com.apollographql.apollo.cache.normalized.Record;
import com.apollographql.apollo.cache.normalized.internal.CacheFieldValueResolver;
import com.apollographql.apollo.cache.normalized.internal.CacheKeyBuilder;
import com.apollographql.apollo.cache.normalized.internal.ReadableStore;
import com.apollographql.apollo.cache.normalized.internal.RealCacheKeyBuilder;
import com.apollographql.apollo.cache.normalized.internal.ResponseNormalizer;
import com.apollographql.apollo.cache.normalized.internal.Transaction;
import com.apollographql.apollo.cache.normalized.internal.WriteableStore;
import com.apollographql.apollo.internal.response.RealResponseReader;
import com.apollographql.apollo.internal.response.RealResponseWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.WeakHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public final class RealApolloStore implements ApolloStore, ReadableStore, WriteableStore {
    private final CacheKeyBuilder cacheKeyBuilder = new RealCacheKeyBuilder();
    final CacheKeyResolver cacheKeyResolver;
    private final Executor dispatcher;
    private final ReadWriteLock lock = new ReentrantReadWriteLock();
    final ApolloLogger logger;
    final OptimisticNormalizedCache optimisticCache;
    final ScalarTypeAdapters scalarTypeAdapters;
    private final Set<RecordChangeSubscriber> subscribers = Collections.newSetFromMap(new WeakHashMap());

    public RealApolloStore(NormalizedCache normalizedCache, CacheKeyResolver cacheKeyResolver2, ScalarTypeAdapters scalarTypeAdapters2, Executor dispatcher2, ApolloLogger logger2) {
        Utils.checkNotNull(normalizedCache, "cacheStore == null");
        this.optimisticCache = (OptimisticNormalizedCache) new OptimisticNormalizedCache().chain(normalizedCache);
        this.cacheKeyResolver = (CacheKeyResolver) Utils.checkNotNull(cacheKeyResolver2, "cacheKeyResolver == null");
        this.scalarTypeAdapters = (ScalarTypeAdapters) Utils.checkNotNull(scalarTypeAdapters2, "scalarTypeAdapters == null");
        this.dispatcher = (Executor) Utils.checkNotNull(dispatcher2, "dispatcher == null");
        this.logger = (ApolloLogger) Utils.checkNotNull(logger2, "logger == null");
    }

    @Override // com.apollographql.apollo.cache.normalized.ApolloStore
    public ResponseNormalizer<Map<String, Object>> networkResponseNormalizer() {
        return new ResponseNormalizer<Map<String, Object>>() {
            /* class com.apollographql.apollo.internal.RealApolloStore.AnonymousClass1 */

            public CacheKey resolveCacheKey(ResponseField field, Map<String, Object> record) {
                return RealApolloStore.this.cacheKeyResolver.fromFieldRecordSet(field, record);
            }

            @Override // com.apollographql.apollo.cache.normalized.internal.ResponseNormalizer
            public CacheKeyBuilder cacheKeyBuilder() {
                return RealApolloStore.this.cacheKeyBuilder;
            }
        };
    }

    @Override // com.apollographql.apollo.cache.normalized.ApolloStore
    public ResponseNormalizer<Record> cacheResponseNormalizer() {
        return new ResponseNormalizer<Record>() {
            /* class com.apollographql.apollo.internal.RealApolloStore.AnonymousClass2 */

            public CacheKey resolveCacheKey(ResponseField field, Record record) {
                return new CacheKey(record.key());
            }

            @Override // com.apollographql.apollo.cache.normalized.internal.ResponseNormalizer
            public CacheKeyBuilder cacheKeyBuilder() {
                return RealApolloStore.this.cacheKeyBuilder;
            }
        };
    }

    @Override // com.apollographql.apollo.cache.normalized.ApolloStore
    public synchronized void subscribe(RecordChangeSubscriber subscriber) {
        this.subscribers.add(subscriber);
    }

    @Override // com.apollographql.apollo.cache.normalized.ApolloStore
    public synchronized void unsubscribe(RecordChangeSubscriber subscriber) {
        this.subscribers.remove(subscriber);
    }

    @Override // com.apollographql.apollo.cache.normalized.ApolloStore
    public void publish(Set<String> changedKeys) {
        Set<RecordChangeSubscriber> iterableSubscribers;
        Utils.checkNotNull(changedKeys, "changedKeys == null");
        if (!changedKeys.isEmpty()) {
            synchronized (this) {
                iterableSubscribers = new LinkedHashSet<>(this.subscribers);
            }
            RuntimeException firstException = null;
            for (RecordChangeSubscriber subscriber : iterableSubscribers) {
                try {
                    subscriber.onCacheRecordsChanged(changedKeys);
                } catch (RuntimeException e) {
                    if (firstException == null) {
                        firstException = e;
                    }
                }
            }
            if (firstException != null) {
                throw firstException;
            }
        }
    }

    @Override // com.apollographql.apollo.cache.normalized.ApolloStore
    public ApolloStoreOperation<Boolean> clearAll() {
        return new ApolloStoreOperation<Boolean>(this.dispatcher) {
            /* class com.apollographql.apollo.internal.RealApolloStore.AnonymousClass3 */

            @Override // com.apollographql.apollo.cache.normalized.ApolloStoreOperation
            public Boolean perform() {
                return (Boolean) RealApolloStore.this.writeTransaction(new Transaction<WriteableStore, Boolean>() {
                    /* class com.apollographql.apollo.internal.RealApolloStore.AnonymousClass3.AnonymousClass1 */

                    public Boolean execute(WriteableStore cache) {
                        RealApolloStore.this.optimisticCache.clearAll();
                        return Boolean.TRUE;
                    }
                });
            }
        };
    }

    @Override // com.apollographql.apollo.cache.normalized.ApolloStore
    public ApolloStoreOperation<Boolean> remove(CacheKey cacheKey) {
        return remove(cacheKey, false);
    }

    @Override // com.apollographql.apollo.cache.normalized.ApolloStore
    public ApolloStoreOperation<Boolean> remove(final CacheKey cacheKey, final boolean cascade) {
        Utils.checkNotNull(cacheKey, "cacheKey == null");
        return new ApolloStoreOperation<Boolean>(this.dispatcher) {
            /* class com.apollographql.apollo.internal.RealApolloStore.AnonymousClass4 */

            /* access modifiers changed from: protected */
            @Override // com.apollographql.apollo.cache.normalized.ApolloStoreOperation
            public Boolean perform() {
                return (Boolean) RealApolloStore.this.writeTransaction(new Transaction<WriteableStore, Boolean>() {
                    /* class com.apollographql.apollo.internal.RealApolloStore.AnonymousClass4.AnonymousClass1 */

                    public Boolean execute(WriteableStore cache) {
                        return Boolean.valueOf(RealApolloStore.this.optimisticCache.remove(cacheKey, cascade));
                    }
                });
            }
        };
    }

    @Override // com.apollographql.apollo.cache.normalized.ApolloStore
    public ApolloStoreOperation<Integer> remove(final List<CacheKey> cacheKeys) {
        Utils.checkNotNull(cacheKeys, "cacheKey == null");
        return new ApolloStoreOperation<Integer>(this.dispatcher) {
            /* class com.apollographql.apollo.internal.RealApolloStore.AnonymousClass5 */

            /* access modifiers changed from: protected */
            @Override // com.apollographql.apollo.cache.normalized.ApolloStoreOperation
            public Integer perform() {
                return (Integer) RealApolloStore.this.writeTransaction(new Transaction<WriteableStore, Integer>() {
                    /* class com.apollographql.apollo.internal.RealApolloStore.AnonymousClass5.AnonymousClass1 */

                    public Integer execute(WriteableStore cache) {
                        int count = 0;
                        for (CacheKey cacheKey : cacheKeys) {
                            if (RealApolloStore.this.optimisticCache.remove(cacheKey)) {
                                count++;
                            }
                        }
                        return Integer.valueOf(count);
                    }
                });
            }
        };
    }

    @Override // com.apollographql.apollo.cache.normalized.ApolloStore
    public <R> R readTransaction(Transaction<ReadableStore, R> transaction) {
        this.lock.readLock().lock();
        try {
            return transaction.execute(this);
        } finally {
            this.lock.readLock().unlock();
        }
    }

    @Override // com.apollographql.apollo.cache.normalized.ApolloStore
    public <R> R writeTransaction(Transaction<WriteableStore, R> transaction) {
        this.lock.writeLock().lock();
        try {
            return transaction.execute(this);
        } finally {
            this.lock.writeLock().unlock();
        }
    }

    @Override // com.apollographql.apollo.cache.normalized.ApolloStore
    public NormalizedCache normalizedCache() {
        return this.optimisticCache;
    }

    @Override // com.apollographql.apollo.cache.normalized.internal.ReadableStore
    public Record read(String key, CacheHeaders cacheHeaders) {
        return this.optimisticCache.loadRecord((String) Utils.checkNotNull(key, "key == null"), cacheHeaders);
    }

    @Override // com.apollographql.apollo.cache.normalized.internal.ReadableStore
    public Collection<Record> read(Collection<String> keys, CacheHeaders cacheHeaders) {
        return this.optimisticCache.loadRecords((Collection) Utils.checkNotNull(keys, "keys == null"), cacheHeaders);
    }

    @Override // com.apollographql.apollo.cache.normalized.internal.WriteableStore
    public Set<String> merge(Collection<Record> recordSet, CacheHeaders cacheHeaders) {
        return this.optimisticCache.merge((Collection) Utils.checkNotNull(recordSet, "recordSet == null"), cacheHeaders);
    }

    @Override // com.apollographql.apollo.cache.normalized.internal.WriteableStore
    public Set<String> merge(Record record, CacheHeaders cacheHeaders) {
        return this.optimisticCache.merge((Record) Utils.checkNotNull(record, "record == null"), cacheHeaders);
    }

    @Override // com.apollographql.apollo.cache.normalized.ApolloStore
    public CacheKeyResolver cacheKeyResolver() {
        return this.cacheKeyResolver;
    }

    @Override // com.apollographql.apollo.cache.normalized.ApolloStore
    public <D extends Operation.Data, T, V extends Operation.Variables> ApolloStoreOperation<T> read(final Operation<D, T, V> operation) {
        Utils.checkNotNull(operation, "operation == null");
        return new ApolloStoreOperation<T>(this.dispatcher) {
            /* class com.apollographql.apollo.internal.RealApolloStore.AnonymousClass6 */

            /* access modifiers changed from: protected */
            @Override // com.apollographql.apollo.cache.normalized.ApolloStoreOperation
            public T perform() {
                return (T) RealApolloStore.this.doRead(operation);
            }
        };
    }

    @Override // com.apollographql.apollo.cache.normalized.ApolloStore
    public <D extends Operation.Data, T, V extends Operation.Variables> ApolloStoreOperation<Response<T>> read(final Operation<D, T, V> operation, final ResponseFieldMapper<D> responseFieldMapper, final ResponseNormalizer<Record> responseNormalizer, final CacheHeaders cacheHeaders) {
        Utils.checkNotNull(operation, "operation == null");
        Utils.checkNotNull(responseNormalizer, "responseNormalizer == null");
        return new ApolloStoreOperation<Response<T>>(this.dispatcher) {
            /* class com.apollographql.apollo.internal.RealApolloStore.AnonymousClass7 */

            /* access modifiers changed from: protected */
            @Override // com.apollographql.apollo.cache.normalized.ApolloStoreOperation
            public Response<T> perform() {
                return RealApolloStore.this.doRead(operation, responseFieldMapper, responseNormalizer, cacheHeaders);
            }
        };
    }

    @Override // com.apollographql.apollo.cache.normalized.ApolloStore
    public <F extends GraphqlFragment> ApolloStoreOperation<F> read(final ResponseFieldMapper<F> responseFieldMapper, final CacheKey cacheKey, final Operation.Variables variables) {
        Utils.checkNotNull(responseFieldMapper, "responseFieldMapper == null");
        Utils.checkNotNull(cacheKey, "cacheKey == null");
        Utils.checkNotNull(variables, "variables == null");
        return new ApolloStoreOperation<F>(this.dispatcher) {
            /* class com.apollographql.apollo.internal.RealApolloStore.AnonymousClass8 */

            /* access modifiers changed from: protected */
            @Override // com.apollographql.apollo.cache.normalized.ApolloStoreOperation
            public F perform() {
                return (F) RealApolloStore.this.doRead(responseFieldMapper, cacheKey, variables);
            }
        };
    }

    @Override // com.apollographql.apollo.cache.normalized.ApolloStore
    public <D extends Operation.Data, T, V extends Operation.Variables> ApolloStoreOperation<Set<String>> write(final Operation<D, T, V> operation, final D operationData) {
        Utils.checkNotNull(operation, "operation == null");
        Utils.checkNotNull(operationData, "operationData == null");
        return new ApolloStoreOperation<Set<String>>(this.dispatcher) {
            /* class com.apollographql.apollo.internal.RealApolloStore.AnonymousClass9 */

            /* access modifiers changed from: protected */
            @Override // com.apollographql.apollo.cache.normalized.ApolloStoreOperation
            public Set<String> perform() {
                return RealApolloStore.this.doWrite(operation, operationData, false, null);
            }
        };
    }

    @Override // com.apollographql.apollo.cache.normalized.ApolloStore
    public <D extends Operation.Data, T, V extends Operation.Variables> ApolloStoreOperation<Boolean> writeAndPublish(final Operation<D, T, V> operation, final D operationData) {
        return new ApolloStoreOperation<Boolean>(this.dispatcher) {
            /* class com.apollographql.apollo.internal.RealApolloStore.AnonymousClass10 */

            /* access modifiers changed from: protected */
            @Override // com.apollographql.apollo.cache.normalized.ApolloStoreOperation
            public Boolean perform() {
                RealApolloStore.this.publish(RealApolloStore.this.doWrite(operation, operationData, false, null));
                return Boolean.TRUE;
            }
        };
    }

    @Override // com.apollographql.apollo.cache.normalized.ApolloStore
    public ApolloStoreOperation<Set<String>> write(final GraphqlFragment fragment, final CacheKey cacheKey, final Operation.Variables variables) {
        Utils.checkNotNull(fragment, "fragment == null");
        Utils.checkNotNull(cacheKey, "cacheKey == null");
        Utils.checkNotNull(variables, "operation == null");
        if (!cacheKey.equals(CacheKey.NO_KEY)) {
            return new ApolloStoreOperation<Set<String>>(this.dispatcher) {
                /* class com.apollographql.apollo.internal.RealApolloStore.AnonymousClass11 */

                /* access modifiers changed from: protected */
                @Override // com.apollographql.apollo.cache.normalized.ApolloStoreOperation
                public Set<String> perform() {
                    return (Set) RealApolloStore.this.writeTransaction(new Transaction<WriteableStore, Set<String>>() {
                        /* class com.apollographql.apollo.internal.RealApolloStore.AnonymousClass11.AnonymousClass1 */

                        public Set<String> execute(WriteableStore cache) {
                            return RealApolloStore.this.doWrite(fragment, cacheKey, variables);
                        }
                    });
                }
            };
        }
        throw new IllegalArgumentException("undefined cache key");
    }

    @Override // com.apollographql.apollo.cache.normalized.ApolloStore
    public ApolloStoreOperation<Boolean> writeAndPublish(final GraphqlFragment fragment, final CacheKey cacheKey, final Operation.Variables variables) {
        return new ApolloStoreOperation<Boolean>(this.dispatcher) {
            /* class com.apollographql.apollo.internal.RealApolloStore.AnonymousClass12 */

            /* access modifiers changed from: protected */
            @Override // com.apollographql.apollo.cache.normalized.ApolloStoreOperation
            public Boolean perform() {
                RealApolloStore.this.publish(RealApolloStore.this.doWrite(fragment, cacheKey, variables));
                return Boolean.TRUE;
            }
        };
    }

    @Override // com.apollographql.apollo.cache.normalized.ApolloStore
    public <D extends Operation.Data, T, V extends Operation.Variables> ApolloStoreOperation<Set<String>> writeOptimisticUpdates(final Operation<D, T, V> operation, final D operationData, final UUID mutationId) {
        return new ApolloStoreOperation<Set<String>>(this.dispatcher) {
            /* class com.apollographql.apollo.internal.RealApolloStore.AnonymousClass13 */

            /* access modifiers changed from: protected */
            @Override // com.apollographql.apollo.cache.normalized.ApolloStoreOperation
            public Set<String> perform() {
                return RealApolloStore.this.doWrite(operation, operationData, true, mutationId);
            }
        };
    }

    @Override // com.apollographql.apollo.cache.normalized.ApolloStore
    public <D extends Operation.Data, T, V extends Operation.Variables> ApolloStoreOperation<Boolean> writeOptimisticUpdatesAndPublish(final Operation<D, T, V> operation, final D operationData, final UUID mutationId) {
        return new ApolloStoreOperation<Boolean>(this.dispatcher) {
            /* class com.apollographql.apollo.internal.RealApolloStore.AnonymousClass14 */

            /* access modifiers changed from: protected */
            @Override // com.apollographql.apollo.cache.normalized.ApolloStoreOperation
            public Boolean perform() {
                RealApolloStore.this.publish(RealApolloStore.this.doWrite(operation, operationData, true, mutationId));
                return Boolean.TRUE;
            }
        };
    }

    @Override // com.apollographql.apollo.cache.normalized.ApolloStore
    public ApolloStoreOperation<Set<String>> rollbackOptimisticUpdates(final UUID mutationId) {
        return new ApolloStoreOperation<Set<String>>(this.dispatcher) {
            /* class com.apollographql.apollo.internal.RealApolloStore.AnonymousClass15 */

            /* access modifiers changed from: protected */
            @Override // com.apollographql.apollo.cache.normalized.ApolloStoreOperation
            public Set<String> perform() {
                return (Set) RealApolloStore.this.writeTransaction(new Transaction<WriteableStore, Set<String>>() {
                    /* class com.apollographql.apollo.internal.RealApolloStore.AnonymousClass15.AnonymousClass1 */

                    public Set<String> execute(WriteableStore cache) {
                        return RealApolloStore.this.optimisticCache.removeOptimisticUpdates(mutationId);
                    }
                });
            }
        };
    }

    @Override // com.apollographql.apollo.cache.normalized.ApolloStore
    public ApolloStoreOperation<Boolean> rollbackOptimisticUpdatesAndPublish(final UUID mutationId) {
        return new ApolloStoreOperation<Boolean>(this.dispatcher) {
            /* class com.apollographql.apollo.internal.RealApolloStore.AnonymousClass16 */

            /* access modifiers changed from: protected */
            @Override // com.apollographql.apollo.cache.normalized.ApolloStoreOperation
            public Boolean perform() {
                RealApolloStore.this.publish((Set) RealApolloStore.this.writeTransaction(new Transaction<WriteableStore, Set<String>>() {
                    /* class com.apollographql.apollo.internal.RealApolloStore.AnonymousClass16.AnonymousClass1 */

                    public Set<String> execute(WriteableStore cache) {
                        return RealApolloStore.this.optimisticCache.removeOptimisticUpdates(mutationId);
                    }
                }));
                return Boolean.TRUE;
            }
        };
    }

    /* access modifiers changed from: package-private */
    public <D extends Operation.Data, T, V extends Operation.Variables> T doRead(final Operation<D, T, V> operation) {
        return (T) readTransaction(new Transaction<ReadableStore, T>() {
            /* class com.apollographql.apollo.internal.RealApolloStore.AnonymousClass17 */

            public T execute(ReadableStore cache) {
                Record rootRecord = cache.read(CacheKeyResolver.rootKeyForOperation(operation).key(), CacheHeaders.NONE);
                if (rootRecord == null) {
                    return null;
                }
                return (T) operation.wrapData((D) operation.responseFieldMapper().map(new RealResponseReader(operation.variables(), rootRecord, new CacheFieldValueResolver(cache, operation.variables(), RealApolloStore.this.cacheKeyResolver(), CacheHeaders.NONE, RealApolloStore.this.cacheKeyBuilder), RealApolloStore.this.scalarTypeAdapters, ResponseNormalizer.NO_OP_NORMALIZER)));
            }
        });
    }

    /* access modifiers changed from: package-private */
    public <D extends Operation.Data, T, V extends Operation.Variables> Response<T> doRead(final Operation<D, T, V> operation, final ResponseFieldMapper<D> responseFieldMapper, final ResponseNormalizer<Record> responseNormalizer, final CacheHeaders cacheHeaders) {
        return (Response) readTransaction(new Transaction<ReadableStore, Response<T>>() {
            /* class com.apollographql.apollo.internal.RealApolloStore.AnonymousClass18 */

            public Response<T> execute(ReadableStore cache) {
                Record rootRecord = cache.read(CacheKeyResolver.rootKeyForOperation(operation).key(), cacheHeaders);
                if (rootRecord == null) {
                    return (Response<T>) Response.builder(operation).fromCache(true).build();
                }
                RealResponseReader<Record> responseReader = new RealResponseReader<>(operation.variables(), rootRecord, new CacheFieldValueResolver(cache, operation.variables(), RealApolloStore.this.cacheKeyResolver(), cacheHeaders, RealApolloStore.this.cacheKeyBuilder), RealApolloStore.this.scalarTypeAdapters, responseNormalizer);
                try {
                    responseNormalizer.willResolveRootQuery(operation);
                    return (Response<T>) Response.builder(operation).data(operation.wrapData( responseFieldMapper.map(responseReader))).fromCache(true).dependentKeys(responseNormalizer.dependentKeys()).build();
                } catch (Exception e) {
                    RealApolloStore.this.logger.e(e, "Failed to read cache response", new Object[0]);
                    return (Response<T>) Response.builder(operation).fromCache(true).build();
                }
            }
        });
    }

    /* access modifiers changed from: package-private */
    public <F extends GraphqlFragment> F doRead(final ResponseFieldMapper<F> responseFieldMapper, final CacheKey cacheKey, final Operation.Variables variables) {
        return (F) ((GraphqlFragment) readTransaction(new Transaction<ReadableStore, F>() {
            /* class com.apollographql.apollo.internal.RealApolloStore.AnonymousClass19 */

            public F execute(ReadableStore cache) {
                Record rootRecord = cache.read(cacheKey.key(), CacheHeaders.NONE);
                if (rootRecord == null) {
                    return null;
                }
                return (F) ((GraphqlFragment) responseFieldMapper.map(new RealResponseReader(variables, rootRecord, new CacheFieldValueResolver(cache, variables, RealApolloStore.this.cacheKeyResolver(), CacheHeaders.NONE, RealApolloStore.this.cacheKeyBuilder), RealApolloStore.this.scalarTypeAdapters, ResponseNormalizer.NO_OP_NORMALIZER)));
            }
        }));
    }

    /* access modifiers changed from: package-private */
    public <D extends Operation.Data, T, V extends Operation.Variables> Set<String> doWrite(final Operation<D, T, V> operation, final D operationData, final boolean optimistic, final UUID mutationId) {
        return (Set) writeTransaction(new Transaction<WriteableStore, Set<String>>() {
            /* class com.apollographql.apollo.internal.RealApolloStore.AnonymousClass20 */

            public Set<String> execute(WriteableStore cache) {
                RealResponseWriter responseWriter = new RealResponseWriter(operation.variables(), RealApolloStore.this.scalarTypeAdapters);
                ResponseNormalizer<Map<String, Object>> responseNormalizer = RealApolloStore.this.networkResponseNormalizer();
                responseNormalizer.willResolveRootQuery(operation);
                responseWriter.resolveFields(responseNormalizer);
                if (!optimistic) {
                    return RealApolloStore.this.optimisticCache.merge(responseNormalizer.records(), CacheHeaders.NONE);
                }
                List<Record> updatedRecords = new ArrayList<>();
                for (Record record : responseNormalizer.records()) {
                    updatedRecords.add(record.toBuilder().mutationId(mutationId).build());
                }
                return RealApolloStore.this.optimisticCache.mergeOptimisticUpdates(updatedRecords);
            }
        });
    }

    /* access modifiers changed from: package-private */
    public Set<String> doWrite(final GraphqlFragment fragment, final CacheKey cacheKey, final Operation.Variables variables) {
        return (Set) writeTransaction(new Transaction<WriteableStore, Set<String>>() {
            /* class com.apollographql.apollo.internal.RealApolloStore.AnonymousClass21 */

            public Set<String> execute(WriteableStore cache) {
                RealResponseWriter responseWriter = new RealResponseWriter(variables, RealApolloStore.this.scalarTypeAdapters);
                ResponseNormalizer<Map<String, Object>> responseNormalizer = RealApolloStore.this.networkResponseNormalizer();
                responseNormalizer.willResolveRecord(cacheKey);
                responseWriter.resolveFields(responseNormalizer);
                return RealApolloStore.this.merge(responseNormalizer.records(), CacheHeaders.NONE);
            }
        });
    }
}
